package com.dom.stor.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dom.common.constants.ConstantError;
import com.dom.common.exception.ServiceException;
import com.dom.stor.dao.entity.TsStockInDetailFlow;
import com.dom.stor.dao.entity.TsStorGoodsDetailInfo;
import com.dom.stor.dao.mapper.TsStorGoodsDetailInfoMapper;
import com.dom.stor.dao.service.ITsStorGoodsDetailInfoService;
import com.xiaoleilu.hutool.util.ObjectUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * B_库存商品明细表 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2019-01-01
 */
@Service
public class TsStorGoodsDetailInfoServiceImpl extends ServiceImpl<TsStorGoodsDetailInfoMapper, TsStorGoodsDetailInfo> implements ITsStorGoodsDetailInfoService {
    @Override
    public boolean addStorageDetail(int memCode, Integer storId, Integer goodCode, BigDecimal amount) {
        TsStorGoodsDetailInfo entity = new TsStorGoodsDetailInfo();
        entity.setMemCode(memCode);
        entity.setStorId(storId);
        entity.setGoodCode(goodCode);
        entity.setAmount(amount);
        return false;
    }

    @Override
    public boolean addStorageDetail(int memCode, Integer storId, List<TsStockInDetailFlow> stockInDetailFlows) {
        List<TsStorGoodsDetailInfo> storGoodsDetailInfos = new ArrayList<>();
        stockInDetailFlows.forEach(stockInDetailFlow ->{
            TsStorGoodsDetailInfo storageDetail = new TsStorGoodsDetailInfo();
            storageDetail.setMemCode(memCode);
            storageDetail.setStorId(storId);
            storageDetail.setGoodCode(stockInDetailFlow.getGoodCode());
            storageDetail.setGoodName(stockInDetailFlow.getGoodName());
            storageDetail.setVarietyCode(stockInDetailFlow.getVarietyCode());
            storageDetail.setStandardName(stockInDetailFlow.getStandardName());
            storageDetail.setAmount(stockInDetailFlow.getAmount());
            storageDetail.setUsbAmt(stockInDetailFlow.getAmount());
            storageDetail.setRemainAmt(stockInDetailFlow.getAmount());

            storGoodsDetailInfos.add(storageDetail);
        });
        return this.insertBatch(storGoodsDetailInfos);
    }

    @Override
    public boolean freezeStorageDetail(int memCode, Integer storId, Integer goodCode, BigDecimal amount) {
        Wrapper<TsStorGoodsDetailInfo> wrapper = buildWhere(memCode, storId, goodCode);
        TsStorGoodsDetailInfo entity = this.selectOne(wrapper);
        if (ObjectUtil.isNull(entity)) {
            throw new ServiceException(ConstantError.DATAERROR);
        }

        entity.setUsbAmt(entity.getUsbAmt().subtract(amount));
        entity.setFrozAmt(entity.getFrozAmt().add(amount));
        return this.updateById(entity);
    }

    @Override
    public boolean unfreezeStorageDetail(int memCode, Integer storId, Integer goodCode, BigDecimal amount) {
        Wrapper<TsStorGoodsDetailInfo> wrapper = buildWhere(memCode, storId, goodCode);
        TsStorGoodsDetailInfo entity = this.selectOne(wrapper);
        if (ObjectUtil.isNull(entity)) {
            throw new ServiceException(ConstantError.DATAERROR);
        }

        entity.setUsbAmt(entity.getUsbAmt().add(amount));
        entity.setFrozAmt(entity.getFrozAmt().subtract(amount));
        return this.updateById(entity);
    }

    @Override
    public boolean reduceStorageDetail(int memCode, Integer storId, Integer goodCode, BigDecimal amount) {
        Wrapper<TsStorGoodsDetailInfo> wrapper = buildWhere(memCode, storId, goodCode);
        TsStorGoodsDetailInfo entity = this.selectOne(wrapper);
        if (ObjectUtil.isNull(entity)) {
            throw new ServiceException(ConstantError.DATAERROR);
        }

        entity.setFrozAmt(entity.getFrozAmt().subtract(amount));
        entity.setOutAmt(entity.getOutAmt().subtract(amount));
        return this.updateById(entity);
    }

    private Wrapper<TsStorGoodsDetailInfo> buildWhere(int memCode, Integer storId, Integer goodCode) {
        Wrapper<TsStorGoodsDetailInfo> wrapper = new EntityWrapper<>();
        return wrapper.eq("mem_code", memCode)
                .eq("stor_id", storId)
                .and().eq("good_code", goodCode);
    }
}
